package com.itheima.wallet.web.Controller.impl;

import com.itheima.wallet.api.AccountService;
import com.itheima.wallet.api.TradeService;
import com.itheima.wallet.common.domain.Account;
import com.itheima.wallet.common.domain.Trade;
import com.itheima.wallet.common.vo.ResponseResult;
import com.itheima.wallet.web.Controller.WalletController;
import com.itheima.wallet.web.service.WalletService;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("wallet")
public class WalletControllerImpl implements WalletController {

    @Reference
    private AccountService accountService;
    
    @Autowired
    private WalletService walletService;

    /**
     * 查询账户信息
     *
     * @param token
     * @return
     */
    @Override
    @GetMapping("queryAccount")
    public ResponseEntity<ResponseResult> queryAccount(@RequestHeader("token") String token) {
        try {
            //查询账户信息
            Account account = accountService.queryAccount(token);

            ResponseResult responseResult = null;
            if (account == null) {
                //token过期或不合法
                responseResult = ResponseResult.errorResult(501, "没有查询到用户");
            } else {
                //封装账户信息
                responseResult = ResponseResult.okResult(account);
            }
            //返回结果
            return ResponseEntity.ok(responseResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ResponseResult responseResult = ResponseResult.errorResult(404, "服务器开小差了，请稍后重试");
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseResult);
    }

    /**
     * 充值入口
     *
     * @param money 存入金额
     * @param token token
     * @return
     */
    @Override
    @PostMapping("saveMoney")
    public ResponseEntity<ResponseResult> saveMoney(Double money,@RequestHeader("token") String token) {
        try {
            //交由Service处理
            Account account = walletService.saveMoney(money,token);

            ResponseResult responseResult = null;
            if (account == null) {
                //token过期或不合法
                responseResult = ResponseResult.errorResult(501, "用户或金额错误");
            } else {
                //封装账户信息
                responseResult = ResponseResult.okResult(account);
            }
            //返回结果
            return ResponseEntity.ok(responseResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ResponseResult responseResult = ResponseResult.errorResult(404, "服务器开小差了，请稍后重试");
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseResult);
    }

    /**
     * 根据token取钱
     *
     * @param money
     * @param token
     * @return
     */
    @Override
    @PostMapping("withdrawMoney/{type}")
    public ResponseEntity<ResponseResult> withdrawMoney(Double money,@RequestHeader("token") String token,@PathVariable Integer type) {
        try {
            //交由Service处理
            Account account = walletService.withdrawMoney(money, token, type);

            ResponseResult responseResult = null;
            if (account == null) {
                //token过期或不合法
                responseResult = ResponseResult.errorResult(501, "用户或金额错误");
            } else {
                //封装账户信息
                responseResult = ResponseResult.okResult(account);
            }
            //返回结果
            return ResponseEntity.ok(responseResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ResponseResult responseResult = ResponseResult.errorResult(404, "服务器开小差了，请稍后重试");
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseResult);
    }

    /**
     * 根据token，分页 查询交易记录
     *
     * @param page
     * @param pageSize
     * @param token
     * @return
     */
    @Override
    @GetMapping("queryTradeList/{page}/{pageSize}")
    public ResponseEntity<ResponseResult> queryTradeList(@PathVariable Integer page,@PathVariable Integer pageSize,@RequestHeader("token") String token) {
        try {
            //交由Service处理
            List<Trade> tradeList = walletService.queryTradeList(page,pageSize,token);

            ResponseResult responseResult = null;
            if (tradeList == null) {
                //token过期或不合法
                responseResult = ResponseResult.errorResult(501, "用户或数据错误");
            } else {
                //封装账单信息
                responseResult = ResponseResult.okResult(tradeList);
            }
            //返回结果
            return ResponseEntity.ok(responseResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ResponseResult responseResult = ResponseResult.errorResult(404, "服务器开小差了，请稍后重试");
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseResult);
    }
}
